package com.ruicar.afs.cloud.basic.contractcancel.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.common.entity.BasicMainInfo;
import com.ruicar.afs.cloud.basic.common.entity.BasicProductParam;
import com.ruicar.afs.cloud.basic.common.entity.BasicReceiptRecord;
import com.ruicar.afs.cloud.basic.common.entity.BasicRepaymentPlan;
import com.ruicar.afs.cloud.basic.common.service.BasicMainInfoService;
import com.ruicar.afs.cloud.basic.common.service.BasicProductParamService;
import com.ruicar.afs.cloud.basic.common.service.BasicReceiptRecordService;
import com.ruicar.afs.cloud.basic.common.service.BasicRepaymentPlanService;
import com.ruicar.afs.cloud.basic.proceeds.debit.service.BasicDebitRecordService;
import com.ruicar.afs.cloud.basic.proceeds.pay.entity.BasicPaymentPool;
import com.ruicar.afs.cloud.basic.proceeds.pay.service.BasicPaymentPoolService;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.*;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author xudongdong
 * @date 2020-06-08
 */

@RestController
@AllArgsConstructor
@RequestMapping("/basicCanaelInfo")
public class BasicCancelInfoController {

    private final BasicMainInfoService basicMainInfoService;
    private final BasicDebitRecordService basicDebitRecordService;
    private final BasicPaymentPoolService basicPaymentPoolService;
    private final BasicProductParamService basicProductParamService;
    private final BasicRepaymentPlanService basicRepaymentPlanService;
    private final BasicReceiptRecordService basicReceiptRecordService;

    @PostMapping("/getBasicCancelList")
    @ApiOperation(value = "合同取消校验")
    public IResponse<String> valid(@RequestParam("contractNo") String contractNo, @RequestParam("isSp") Boolean isSp) {
        BigDecimal b = BigDecimal.valueOf(0);
        BasicPaymentPool paymentPool = basicPaymentPoolService.getOne(Wrappers.<BasicPaymentPool>lambdaQuery()
                .eq(BasicPaymentPool::getContractNo, contractNo));
        if (paymentPool == null) {
            return new IResponse().setData("付款池无该数据，请核实！");
        }
        if (isSp) {
            BasicProductParam basicProductParam = basicProductParamService.getOne(Wrappers.<BasicProductParam>lambdaQuery()
                    .eq(BasicProductParam::getContractNo, contractNo)
                    .eq(BasicProductParam::getAtomKey, AfsEnumUtil.key(ProductAtomEnum.CONTRACT_LIMIT_DAYS)));
            Date newDate;
            Calendar ca = Calendar.getInstance();
            ca.add(Calendar.DATE, 0 - Integer.valueOf(basicProductParam.getAtomValue()));
            newDate = ca.getTime();
            BasicMainInfo basicMainInfo = basicMainInfoService.getOne(Wrappers.<BasicMainInfo>lambdaQuery()
                    .eq(BasicMainInfo::getContractNo, contractNo)
                    .ge(BasicMainInfo::getStartDate, newDate));
            if (basicMainInfo == null) {
                return new IResponse().setData("激活时间已超过" + basicProductParam.getAtomValue() + "天无法合同取消！");
            } else if (ContractStatusEnum.contractCancel.equals(basicMainInfo.getContractStatus())) {
                return new IResponse().setData("该合同已取消！");
            } else if (ContractStatusEnum.overdue.equals(basicMainInfo.getContractStatus())) {
                return new IResponse().setData("逾期合同无法取消！");
            }
            if (!PaymentStatusEnum.successPayment.equals(paymentPool.getPaymentStatus())) {
                return new IResponse().setData("经销商只能取消已放款的合同！");
            }
        } else {
            BasicRepaymentPlan basicRepaymentPlan = basicRepaymentPlanService.getOne(Wrappers.<BasicRepaymentPlan>lambdaQuery()
                    .eq(BasicRepaymentPlan::getContractNo, contractNo)
                    .eq(BasicRepaymentPlan::getTermNo, 1));
            List<BasicReceiptRecord> basicReceiptRecord = basicReceiptRecordService.list(Wrappers.<BasicReceiptRecord>lambdaQuery()
                    .eq(BasicReceiptRecord::getContractNo, contractNo)
                    .notIn(BasicReceiptRecord::getReceiptStatus, AfsEnumUtil.key(ReceiptStatusEnum.revoke)));
            for (BasicReceiptRecord basicReceipt : basicReceiptRecord) {
                if (basicReceipt.getUnusedAmt().compareTo(b) > 0) {
                    return new IResponse().setData("该合同已提前还款无法合同取消！");
                }
            }
            Date date = new Date();
            if (date.after(basicRepaymentPlan.getDueDate())) {
                return new IResponse().setData("已超过首期还款日无法合同取消！");
            } else if (RepaymentStatusEnum.PREPAYMENT.equals(basicRepaymentPlan.getStatus())) {
                return new IResponse().setData("该合同已提前结清无法合同取消！");
            }
        }
        if (PaymentStatusEnum.paying.equals(paymentPool.getPaymentStatus())) {
            return new IResponse().setData("该合同正在放款中请勿取消！");
        } else if (PaymentStatusEnum.contractCancel.equals(paymentPool.getPaymentStatus())) {
            return new IResponse().setData("该合同已取消！");
        }
        return new IResponse().setData(Boolean.TRUE.toString());
    }
}
